package com.business.sectiondetail.controller;

import java.io.File;
import java.util.Date;

import com.business.section.validator.SectionValidator;
import com.common.MyCaptchaRender;
import com.common.util.picture.PictureBase64;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.model.SectionDetail;
import com.model.User;

/**
 * 回帖
 * 
 * @author orange
 *
 */
public class SectionDetailController extends Controller {
	// 新增
	public void save() {
		getFile("contextFile");
		User user = (User) getSession().getAttribute("user");
		SectionDetail sectiondetail = new SectionDetail();
		sectiondetail.setContext(getPara("context"));
		sectiondetail.setSid(Long.parseLong(getPara("sid")));
		@SuppressWarnings("deprecation")
		String context = PictureBase64.getShtmlContent(sectiondetail.getContext(),
				PropKit.get("uploadFileUrl") + File.separator + "context", getRequest().getRealPath("/"),
				getRequest().getContextPath());
		sectiondetail.setContext(context);
		sectiondetail.setCreateTime(new Date());
		sectiondetail.setUpdateTime(new Date());
		sectiondetail.setItStatus(1);
		sectiondetail.setUserid(user.getId());
		sectiondetail.save();

		// 做等级统计
		user.setIntegral(user.getIntegral() + 1);// 回帖经验+1
		if (user.getUserLevel() > 1) {
			if (user.getIntegral() >= (user.getUserLevel() - 1) * 100) {
				user.setIntegral(user.getIntegral() - (user.getUserLevel() - 1) * 100);
				user.setUserLevel(user.getUserLevel() + 1);
			}
		} else {
			if (user.getUserLevel() > 0) {
				if (user.getIntegral() >= 50) {
					user.setIntegral(user.getIntegral() - 50);
					user.setUserLevel(user.getUserLevel() + 1);
				}
			} else {
				if (user.getIntegral() >= 10) {
					user.setIntegral(user.getIntegral() - 10);
					user.setUserLevel(user.getUserLevel() + 1);
				}
			}
		}
		user.update();
		redirect("/sectiondetail/"+getPara("sid"));
	}

	// 删除
	@Before(SectionValidator.class)
	public void delete() {

	}

	// 修改
	public void edit() {

	}

	// 列表
	public void index() {
		String sqloption = "SELECT `user`.id AS 'user.id',`user`.nick_name AS 'user.nickName', "
				+ "`user`.head_portrait_img AS 'user.headPortraitImg',"
				+ "`section`.`id` AS 'section.id',`section`.`name` AS 'section.name',"
				+ "`section`.context AS 'section.context'," + "`section`.section_img AS 'section.sectionImg'";
		String sql = " FROM " + "`user` INNER JOIN " + "`section` ON(`section`.user_id = `user`.id) WHERE `section`.id="
				+ getParaToLong(0) + " ORDER BY `section`.id desc";
		Page<Record> userAndSectionRecord = Db.paginate(1, 1, sqloption, sql);
		setAttr("sectionPage", userAndSectionRecord);
		String sqloption1 = "SELECT `user`.id AS 'user.id'," + "`user`.nick_name AS 'user.nickName', "
				+ "`user`.head_portrait_img AS 'user.headPortraitImg',"
				+ "`section_detail`.`context` AS 'sectionDetail.context',"
				+ "`section_detail`.create_time AS 'sectionDetail.createTime'";
		String sql1 = " FROM " + "`section_detail` INNER JOIN "
				+ "`user` ON(`section_detail`.userid = `user`.id) WHERE `section_detail`.sid=" + getParaToLong(0)
				+ " ORDER BY `section_detail`.create_time desc";
		Page<Record> userAndSectionDetailRecord = Db.paginate(getParaToInt(1, 1), 5, sqloption1, sql1);
		setAttr("sectionDetailPage", userAndSectionDetailRecord);
		render("add.html");
	}
}
